import SwiftUI

struct GeocodingProgressView: View {
    @ObservedObject var photoService: PhotoService
    
    var body: some View {
        VStack(spacing: 20) {
            // 标题
            Text("正在处理地理信息")
                .font(.title2)
                .fontWeight(.semibold)
                .foregroundColor(.primary)
            
            // 进度条
            VStack(spacing: 10) {
                ProgressView(value: photoService.geocodingProgress)
                    .progressViewStyle(LinearProgressViewStyle(tint: .blue))
                    .scaleEffect(x: 1, y: 2, anchor: .center)
                
                // 进度文本
                Text("\(photoService.geocodingCurrentPhoto) / \(photoService.geocodingTotalPhotos)")
                    .font(.caption)
                    .foregroundColor(.secondary)
            }
            .padding(.horizontal, 40)
            
            // 描述
            Text("正在为照片添加地理位置信息，请稍候...")
                .font(.body)
                .foregroundColor(.secondary)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 40)
            
            // 状态信息
            if photoService.geocodingTotalPhotos > 0 {
                VStack(spacing: 5) {
                    Text("预计剩余时间: \(estimatedTimeRemaining)")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    
                    Text("处理速度: \(processingSpeed) 个/分钟")
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
            }
        }
        .padding()
        .background(Color(.systemBackground))
        .cornerRadius(12)
        .shadow(radius: 5)
    }
    
    // 计算预计剩余时间
    private var estimatedTimeRemaining: String {
        guard photoService.geocodingCurrentPhoto > 0 else { return "计算中..." }
        
        let remaining = photoService.geocodingTotalPhotos - photoService.geocodingCurrentPhoto
        let rate = Double(photoService.geocodingCurrentPhoto) / 60.0 // 假设每分钟处理速度
        let estimatedSeconds = Double(remaining) / rate
        
        if estimatedSeconds < 60 {
            return "\(Int(estimatedSeconds))秒"
        } else {
            let minutes = Int(estimatedSeconds / 60)
            return "\(minutes)分钟"
        }
    }
    
    // 计算处理速度
    private var processingSpeed: String {
        guard photoService.geocodingCurrentPhoto > 0 else { return "0" }
        
        // 基于当前进度估算速度（每分钟）
        let rate = Double(photoService.geocodingCurrentPhoto) / 60.0
        return String(format: "%.1f", rate)
    }
}

#Preview {
    GeocodingProgressView(photoService: PhotoService())
} 